package uk.co.q3c.deplan.server.util.rules;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Id;

import uk.co.q3c.deplan.client.domain.AbstractDomainObject;
import uk.co.q3c.deplan.client.domain.DomainKind;
import uk.co.q3c.deplan.client.domain.DomainObject;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.service.ServiceFactory;
import uk.co.q3c.deplan.server.rcp.layout.WidgetType;
import uk.co.q3c.deplan.server.util.InvalidPropertyException;

import com.googlecode.objectify.Key;

/**
 * Options for the layout and format of an {@link AutoEditor} form, for the instance of a given class to be displayed
 * 
 * @author DSowerby 16 May 2009
 * 
 */
public class ClassLayoutOptions extends AbstractDomainObject implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9002499342079452110L;
	private Map<String, PropertyLayoutOptions> propertyOptions;
	@Id
	private Long keyId;

	public ClassLayoutOptions() {
		super();
	}

	@Override
	protected void beforeLoad() {
		super.beforeLoad();

	}

	/**
	 * @param dao
	 */
	public ClassLayoutOptions(ServiceFactory connection) {
		super(connection);
	}

	/**
	 * Returns the options for the specified property, or null if none are defined.
	 * 
	 * @param propertyName
	 * @return
	 * @throws InvalidPropertyException
	 */
	public PropertyLayoutOptions getPropertyOptions(String propertyName) {

		return propertyOptions.get(propertyName);
	}

	public void setOption(String propertyName, WidgetType widgetType) {

		PropertyLayoutOptions plo = propertyOptions.get(propertyName);
		boolean newEntry = false;
		if (plo == null) {
			plo = new PropertyLayoutOptions();
			newEntry = true;
		}
		plo.setWidgetType(widgetType);
		if (newEntry) {
			propertyOptions.put(propertyName, plo);
		}
	}

	@Override
	public void afterLoad(ServiceFactory connection) {
		super.afterLoad(connection);
		if (propertyOptions == null) {
			propertyOptions = new HashMap<String, PropertyLayoutOptions>();
		}
	}

	@Override
	public DomainKind getKind() {
		throw new RuntimeException("not yet implemented");
	}

	public Long getKeyId() {
		return keyId;
	}

	@Override
	public Key<? extends DomainObject> getKey() throws PersistenceReferenceException {
		throw new RuntimeException("Not yet implemented");
	}

	@Override
	public void setKeyId(Long keyId) {
		throw new RuntimeException("Not yet implemented");
	}
}
